package com.sys.ai.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.ai.domain.AiApps;
import com.sys.ai.domain.AiModel;
import com.sys.ai.domain.AiWorkflow;
import com.sys.ai.mapper.AiAppsMapper;
import com.sys.ai.mapper.AiModelMapper;
import com.sys.ai.mapper.AiWorkflowMapper;
import com.sys.ai.service.IAiAppsService;
import com.sys.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * AI应用Service业务层处理
 *
 * @author LGY
 * @date 2025-05-19
 */
@Slf4j
@Service
public class AiAppsServiceImpl extends ServiceImpl<AiAppsMapper, AiApps> implements IAiAppsService {
    @Autowired
    private AiAppsMapper aiAppsMapper;
    @Autowired
    private AiModelMapper modelMapper;
    @Autowired
    private AiWorkflowMapper workflowMapper;

    /**
     * 查询AI应用
     *
     * @param id AI应用主键
     * @return AI应用
     */
    @Override
    public AiApps selectAiAppsById(String id) {
        return this.getById(id);
    }

    /**
     * 查询AI应用列表
     *
     * @param aiApps AI应用
     * @return AI应用
     */
    @Override
    public List<AiApps> selectAiAppsList(AiApps aiApps) {
        return aiAppsMapper.selectAiAppsList(aiApps);
    }

    /**
     * 新增AI应用
     *
     * @param aiApps AI应用
     * @return 结果
     */

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertAiApps(AiApps aiApps) {
        this.save(aiApps);
    }

    /**
     * 修改AI应用
     *
     * @param aiApps AI应用
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateAiApps(AiApps aiApps) {
        this.updateById(aiApps);
    }

    /**
     * 批量删除AI应用
     *
     * @param ids 需要删除的AI应用主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteAiAppsByIds(String[] ids) {
        this.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    public List<AiApps> listAllReleaseApps(AiApps aiApps) {
        LambdaQueryChainWrapper<AiApps> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(AiApps::getStatus, 1);
        if (!StringUtils.isBlank(aiApps.getName())) {
            lambdaQueryChainWrapper.like(AiApps::getName, aiApps.getName());
        }
        List<AiApps> aiAppsList = lambdaQueryChainWrapper
                .orderByDesc(AiApps::getCreateTime)
                .list();
        if (!CollectionUtils.isEmpty(aiAppsList)) {
            List<String> modelIdList = aiAppsList.stream().map(AiApps::getModelId).toList();
            List<String> flowIdList = aiAppsList.stream().map(AiApps::getWorkflowId).toList();
            Map<String, AiModel> modelMap = new HashMap<>();
            Map<String, AiWorkflow> workflowMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(modelIdList)) {
                List<AiModel> aiModels = modelMapper.selectBatchIds(modelIdList);
                modelMap = aiModels.stream().collect(Collectors.toMap(AiModel::getId, aiModel -> aiModel));
            }
            if (!CollectionUtils.isEmpty(flowIdList)) {
                List<AiWorkflow> aiWorkflows = workflowMapper.selectBatchIds(flowIdList);
                workflowMap = aiWorkflows.stream().collect(Collectors.toMap(AiWorkflow::getId, aiWorkflow -> aiWorkflow));
            }
            for (AiApps apps : aiAppsList) {
                String modelName = StringUtils.isEmpty(apps.getModelId()) ? null : modelMap.get(apps.getModelId()).getName();
                String flowName = StringUtils.isEmpty(apps.getWorkflowId()) ? null : workflowMap.get(apps.getWorkflowId()).getName();
                apps.setModelName(modelName)
                        .setFlowName(flowName);
            }
        }
        return aiAppsList;
    }
}